/*********************************************
Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

Subscribe to see which companies asked this question

*********************************************/


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

bool isValid(char* s) {
	char val[1024];
	int top = -1;
	int i, tmp;

	for(i = 0; *(s+i) != '\0'; i++)
	{
		if(s[i] == '[' || s[i] == '(' || s[i] == '{')
		{
			val[++top] = s[i];
		}
		else
		{
			if(top == -1)
				return false;
			else
			{
				if((s[i] == ']' && val[top] == '[')
				|| (s[i] == ')' && val[top] == '(')
				|| (s[i] == '}' && val[top] == '{'))
				{
					top--;
				}
				else
					return false;
			}
		}
	}
	if(top != -1)
		return false;
	else
		return true;
}


int main(void)
{
	char str[6];
	str[0] = '[';
	str[1] = '(';
	str[2] = '{';
	str[3] = '}';
	str[4] = ')';
	str[5] = ']';
	printf("%d\n",isValid(str));
	return 0;
}
